//
//  CusSwitch.swift
//  CosClub
//
//  Created by 沙睿 on 2020/4/17.
//  Copyright © 2020 ios. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa

public class CusSwith : UIView {
    
    public var onImage: UIImage?
    public var offImage: UIImage?
    public let changeStatus = PublishSubject<Bool>()
    public lazy var leftBtn = {creatButton(title: "公开")}()
    public lazy var rightBtn = {creatButton(title: "私密")}()
    
    public var leftString: String = ""  {
        didSet {
            self.leftBtn.setTitle(self.leftString, for: .normal)
        }
    }
    
    public var rightString: String = ""  {
        didSet {
            self.rightBtn.setTitle(self.rightString, for: .normal)
        }
    }
    
    public var isOn: Bool = false{
        didSet {
            changeStatus()
        }
    }
    
    public func setOn(_ on: Bool, animated: Bool) {
        self.isOn = on
        changeStatus(animal: animated)
    }
    
    
    
    let btnSize = CGSize(width: 32, height: 26)
    let onView = UIImageView()
    let backgroundView = UIImageView()
    
    private let bag = DisposeBag()
    
    public convenience init() {
        self.init(frame:CGRect(x: 0, y: 0, width: 64, height: 26))
        self.isOn = false
    }
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        creatSubviews()
        
        let tap = UITapGestureRecognizer()
        tap.rx.event.asObservable().subscribe(onNext: { [weak self] (_) in
            guard let this = self else {return}
            this.isOn = !this.isOn
            print(this.isOn)
        }).disposed(by: self.bag)
        addGestureRecognizer(tap)
        
    }
    
    public func creatSubviews() {
        addSubview(backgroundView)
        addSubview(onView)
        addSubview(leftBtn)
        addSubview(rightBtn)
        leftBtn.isSelected = true
        rightBtn.isSelected = false
    }
    
    public override func layoutSubviews() {
        super.layoutSubviews()
        var x: CGFloat = 0
        let y: CGFloat = (self.frame.height - btnSize.height)/2
        leftBtn.frame = CGRect(origin: CGPoint(x: x, y: y), size: btnSize)
        
        x = self.frame.width - btnSize.width
        rightBtn.frame = CGRect(origin: CGPoint(x: x, y: y), size: btnSize)
        onView.frame = CGRect(origin: CGPoint(x: 0, y: y), size: btnSize)
        backgroundView.frame = self.bounds
        
        UIImage.color(UIColor.app.c1, size: btnSize,cornerRadius: onView.height/2) {self.onView.image = $0}
        UIImage.color(UIColor.black, size: self.bounds.size, cornerRadius: self.height/2) {self.backgroundView.image = $0}
        
        
    }
    
    public func creatButton(title: String?) -> UIButton{
        let leftBtn = UIButton(title, color: UIColor.white, fontSize: 10)
        leftBtn.setTitleColor(UIColor.app.c3a, for: .selected)
        leftBtn.isUserInteractionEnabled = false
        return leftBtn
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension CusSwith {
    public func changeStatus(animal: Bool = true) {
        var x: CGFloat = 0
        
        if self.isOn {
            x = self.frame.width - self.onView.width
        }
        var duration: TimeInterval = 0
        if animal {
            duration = 0.3
        }
        changeStatus.onNext(self.isOn)
        UIView.animate(withDuration: duration) {
            self.onView.x = x
            if self.isOn {
                self.leftBtn.isSelected = false
                self.rightBtn.isSelected = true
            }else {
                self.rightBtn.isSelected = false
                self.leftBtn.isSelected = true
            }
        }
    }
}
